package leetcode100;

import java.util.HashMap;
import java.util.Map;

// TODO 哈希表 [经典] 【-】 两数之和
// TODO https://leetcode.cn/problems/two-sum/solution/liang-shu-zhi-he-by-leetcode-solution/

public class lc100_0001 {

    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        Map<Integer, Integer> m = new HashMap<>();  // 数组值 为 Key, 下标 为 Value
        int[] res = new int[2];

        for (int i=0; i<n; i++) {
            if (m.containsKey(target - nums[i])) {
                res[0] = (int) m.get(target - nums[i]);
                res[1] = i;
            }
            m.put(nums[i], i);
        }
        return res;
    }

}
